<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ################

$VIEW->template = $dirname.'/templates/thickbox.tpl';

ob_start();

$keyname = preg_replace('/[^a-zA-Z0-9]/', '', get('menu'));

$mData = $DB->fetchRow("SELECT * FROM {$CONFIG->dbprefix}jfxmenu_menus WHERE keyname = '{$keyname}'");

if($mData===false){
    JFX::addError('Invalid menu keyname');
}else{

    if(Antz_IntelliForm::submitted('edit-menu')){
        $error = false;

        $keyname = preg_replace('/[^a-zA-Z0-9]/', '', post('keyname'));
        $title = post('title');
        $template = $_POST['template'];
        $parentid = (int) post('parent');

        if($keyname != $mData['keyname'] && $DB->countRows($CONFIG->dbprefix.'jfxmenu_menus', "keyname = '{$keyname}'")>0){
            $error = true;
            JFX::addError($this->lang('keyname_already_registered'));
            JFX::addFormError('keyname');
        }

        if($DB->countRows($CONFIG->dbprefix.'pages', "id = '{$parentid}'")==0){
            $error = true;
            JFX::addError($this->lang('invalid_parent'));
        }

        if(!$error){
            $dbParams = array(
                'keyname' => $keyname,
                'template' => $template,
                'parent_id' => $parentid
            );
            $this->updateContentLang('title', $keyname, $title, '*', '*');
            $DB->update($CONFIG->dbprefix.'jfxmenu_menus', $dbParams, "keyname = '{$mData['keyname']}'");
            JFX::addSuccess($this->lang('new_menu_success'));
        }




    }



$VIEW->addJs('
$(document).ready(function(){
    $("#jfxcore-edit-menu-tree").treeview({
        persist: "location",
        collapsed: true,
        unique: true
    });
    $("#jfxcore-edit-menu-tree a").click(function(){
        $("#parent-id").get(0).value = this.id.replace("page_", "");
        $("#disabled-parent").get(0).value = $(this).text();
    });
});
');





$pagesRecursive = JFX::getPageTree(0, $USER->workspace);

$tree = '<ul id="jfxcore-edit-menu-tree">'.JFXMenu_MakeTree($pagesRecursive, 'page_').'</ul>';
$parentTitle = $LANG->getContent('core', 'page_title', post('parent', $mData['parent_id']));

    $parent = array(
        'name' => 'parent',
        'value' => post('parent', $mData['parent_id']),
        'type' => 'hidden',
        'id' => 'parent-id',
        'label' => ''
    );
    $parentList = array(
        'type' => 'longtag',
        'tag' => 'div',
        'class' => 'form-field',
        'id' => 'parent-page-tree',
        'content' => '<label>'.$LANG->get('core', 'page_parent').'</label>
<div class="input-area">'.$tree.'</div>'
    );
    $selectedParent = array(
        'name' => 'disabled_parent',
        'value' => $parentTitle,
        'type' => 'text',
        'id' => 'disabled-parent',
        'extra' => array(
            array(
                'command' => 'addAttribute',
                'params' => array(
                    '"disabled"', '"disabled"'
                )
            )
        ),
        'label' => 'Selected Parent'
    );








    $formFields = array(
        array(
            'type' => 'text',
            'name' => 'keyname',
            'value' => post('keyname', $mData['keyname']),
            'label' => $this->lang('edit_keyname')
        ),
        array(
            'type' => 'text',
            'name' => 'title',
            'value' => post('title', $this->getContentLang('title', $mData['keyname'])),
            'label' => $this->lang('edit_title')
        ),
        
    $parent,
    $parentList,
    $selectedParent,
        array(
            'type' => 'code',
            'id' => 'jfxmenunew',
            'class' => 'html',
            'name' => 'template',
            'value' => post('template', $mData['template'], 5000),
            'label' => $this->lang('edit_template')
        ),
        array(
            'type' => 'submit',
            'name' => 'submitBtn',
            'value' => $this->lang('edit_submit'),
            'label' => '&nbsp;'
        )
    );






    echo JFX::makeRapidForm($formFields, 'Edit Menu Details', 'edit-menu');
}

$content = ob_get_contents();
ob_end_clean();

$SMARTY->assign('_CST_BODY', $content.' ------');























function JFXMenu_MakeTree($arr, $idprefix){
    $CONFIG = JFX::registry('config');
    $LANG = JFX::registry('lang');
    $returnString = '';
    foreach($arr as $k=>$v){

        $link = '<a href="javascript: void(null)" id="'.$idprefix.$v['id'].'">'.$LANG->getContent('core', 'page_title', $v['id']).'</a>';
        if(isset($v['subpages']) && count($v['subpages'])>0){
            $subpages = '<ul>'.JFXMenu_MakeTree($v['subpages'], $idprefix).'</ul>';
        }else{
            $subpages = '';
        }
        $returnString .= '<li>'.$link.$subpages.'</li>';
    }

    return $returnString;
}

